<template>
  <div id="AsnDetailList">
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="selectData" @backList="backList" @del="del" @newCreate="newCreate" @saveDeatails="saveDeatails" @back="back"></v-tableTitle>
    <div class="detail">
      <v-navMenu :detailNav="detailNav" :menuData="menuData" @setMentActive="setMentActive" v-if="detailNav != 1">
      </v-navMenu>
      <component :data="{ detailData, argument }" :is="currentComponent"></component>
    </div>
  </div>

</template>

<script>
import tableTitle from 'commons/TableTitle'
import navMenu from 'commons/NavMenu'
import baseMixin from '@/mixins/BaseMixin'
import config from '../Config'

export default {
  name: 'DetailList',
  mixins: [baseMixin, config],
  data() {
    return {
      newFlag: false,
      mainFlag: false,
      detailFlag: false,
      argument: {},
      detailData: {},
      selectData: [],
      menuData: [
        { label: this.$t('common.detail'), index: '2' },
        { label: this.$t('in.udf'), index: '3' }
      ],
      detailNav: '1',
      componentConfig: [
        {
          name: 'Table',
          label: 'table',
          component: () => import('./page/Table.vue')
        },
        {
          name: 'baisc',
          label: this.$t('common.detail'),
          component: () => import('./page/baisc.vue')
        },
        {
          name: 'udf',
          label: this.$t('in.udf'),
          component: () => import('./page/udf.vue')
        }
      ]
    }
  },
  computed: {
    btnConfig() {
      if (this.detailNav == 1) {
        return [
          {
            label: this.$t('common.create'),
            certigier:
              this.newDetailStatus ||
              !this.btnPermNameList.includes('WM010411'),
            invalid: 'newCreate'
          },
          {
            label: this.$t('common.delete'),
            certigier:
              this.deleteDetailStatus ||
              !this.btnPermNameList.includes('WM010412'),
            disabled: true,
            invalid: 'del'
          }
        ]
      } else if (this.$store.state.newFlag) {
        return [
          {
            label: this.$t('app.saveDetail'),
            certigier: !this.btnPermNameList.includes('WM010413'),
            invalid: 'saveDeatails'
          },
          {
            label: this.$t('common.return'),
            certigier: !this.btnPermNameList.includes('WM010414'),
            invalid: 'back'
          }
        ]
      } else {
        return [
          {
            label: this.$t('common.return'),
            certigier: !this.btnPermNameList.includes('WM010414'),
            invalid: 'backList'
          }
        ]
      }
    },
    currentComponent() {
      return this.componentConfig[this.detailNav - 1].component
    }
  },
  methods: {
    //删除数据
    async del(data) {
      try {
        let res = await this.$api.post(`/deletePoDetail`, data)
        
          this.$message.success(res.returnMgr)
          let page = sessionStorage.getItem('page')
          this.$children[1].getTableData(1, {})
      } catch (e) {
        this.$message.error(e)
      }
    },
    //选中的数据
    setSelectData(data) {
      this.selectData = data
    },
    //进入详情
    async setdialog(data) {
      try {
        let res = await this.$api.get(
          `/po/detail/detailed/${this.argument.pokey}/${data.polinenumber}`
        )
        
          this.detailData = res.bean
          this.detailNav = '2'
      } catch (e) {
        this.$message.error(e)
      }
    },
    //返回试图
    backList() {
      this.detailNav = '1'
    },
    //新建
    async newCreate() {
      this.detailData = {
        polinenumber: '00001',
        qtyordered: '0',
        qtyreceived: '0',
        status: '0',
        qcrequired: '0',
        qcautoadjust: '0',
        uom: '',
        pokey: '',
        rtxStylekey: ' ',
        packkey: '',
        sku: '',
        descr: ''
      }
      try {
        let res = await this.$api.get(
          `/po/detail/polinenumber/${this.argument.pokey}`
        )
        
          this.detailData.polinenumber = res.bean
          this.detailData.pokey = this.argument.pokey
          this.detailNav = '2'
      } catch (e) {
        this.$message.error(e)
      }
    },
    //导航值
    setMentActive(key) {
      this.detailNav = key
    },
    //保存明细
    async saveDeatails() {
      this.$parent.$refs['detail'].$children[1].$refs.input.$refs[
        'from'
      ].validate(valid => {
        if (valid) {
          this.mainFlag = true
          this.$children[2].$refs.input.$refs['from'].validate(valid => {
            if (valid) {
              this.detailFlag = true
              if (this.detailFlag && this.mainFlag) {
                //上下数据均已校准 可以开始保存
                let detail = this.$parent.$children[1].detailData
                let detailList = this.detailData
                detailList.storerkey = detail.storerkey
                if (detailList.qtyordered == 0) {
                  this.$message.error(this.$t('msg.msg52'))
                  return
                }
                let data = detail
                data.podetail = []
                data.podetail.push(detailList)
                try {
                  this.$api.post('/savePo', data).then(res => {
                    
                      this.$message.success(res.returnMgr)
                      let str = ''
                      // 查询现在的编号
                      this.$api
                        .get(
                          `/po/detail/polinenumber/${this.argument.receiptkey}`
                        )
                        .then(res1 => {
                            str = res1.bean
                            // 回归第一页 且清空其他页面的所有数据
                            this.detailData = {
                              polinenumber: str,
                              qtyordered: '0',
                              qtyreceived: '0',
                              status: '0',
                              qcrequired: '0',
                              qcautoadjust: '0',
                              uom: '',
                              pokey: detail.pokey,
                              rtxStylekey: ' ',
                              packkey: '',
                              sku: '',
                              descr: ''
                            }
                          
                        })
                  })
                } catch (e) {
                  this.$message.error(e)
                }
              }
            }
          })
        }
      })
    }, // 返回到列表页面
    back() {
      this.$router.push({ name: 'po' })
    }
  },
  mounted() {
    this.newFlag = this.$store.state.newFlag
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip
      this.argument = data.routeData
      this.detailData = data.detailList
      this.detailNav = data.detailListNav
    } else if (this.newFlag) {
      this.argument = {
        receiptkey: this.$store.state.routeId,
        storerkey:""
      }
      this.detailData = {
        polinenumber: '00001',
        qtyordered: '0',
        qtyreceived: '0',
        status: '0',
        qcrequired: '0',
        qcautoadjust: '0',
        uom: '',
        pokey: '',
        rtxStylekey: ' ',
        packkey: '',
        sku: '',
        descr: ''
      }
      this.detailNav = '2'
    } else {
      this.argument = this.$store.state.routeData
    }
  },
  components: {
    'v-tableTitle': tableTitle,
    'v-navMenu': navMenu
  },
  watch: {
    detailNav: {
      handler(data) {
        if (data != '1') {
          this.$store.commit('setTemporary', true)
        }
      },
      immediate: true,
      deep: true
    },
    selectData(data) {
      if (this.permissionStatus) {
        if (data.length > 0) {
          this.deleteDetailStatus = false
        } else {
          this.deleteDetailStatus = true
        }
      }
    }
  }
}
</script>

<style>
</style>
